package com.sw.controller;

import com.github.pagehelper.PageHelper;
import com.sw.exception.InvalidArgumentException;
import com.sw.mapper.OriginatorMapper;
import com.sw.model.Originator;
import com.sw.model.OriginatorExample;
import com.sw.service.OriginatorService;
import com.sw.domain.OriginatorPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/7/12.
 */
@RestController
@RequestMapping(value = "originators")
public class OriginatorController {
    @Autowired
    private OriginatorMapper originatorMapper;
    @Autowired
    private OriginatorService originatorService;

    /**创建组织者
     * @param originator
    * @return
    */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public Originator insert(@RequestBody Originator originator){
        int timestamp=(int)(new Date().getTime()/100);
        originator.setOriginatorId(timestamp);
        originatorMapper.insertSelective(originator);
        return originator;
    }

    /**删除组织者
     * @param originatorId
     *@return
     */
    @RequestMapping(value = "{originatorId}",method = RequestMethod.DELETE)
    public Originator delete(@PathVariable int originatorId){
        Originator originator=originatorMapper.selectByPrimaryKey(originatorId);
        if (originator==null){
            throw new InvalidArgumentException("该组织者不存在，请核对originatorId是否正确");
        }else {
            originator.setOriginatorState(4);
            originatorMapper.updateByPrimaryKeySelective(originator);
            return originatorMapper.selectByPrimaryKey(originatorId);
        }
    }

    /**根据组织者ID修改组织者信息
     * @param originatorId
     * @param originator
     * @return
     */
    @RequestMapping(value = "{originatorId}",method = {RequestMethod.POST,RequestMethod.PUT})
    public Originator update(@PathVariable int originatorId ,@RequestBody Originator originator){
        if (originatorMapper.selectByPrimaryKey(originatorId)==null){
            throw new InvalidArgumentException("该组织者不存在，请核对originatorId是否正确");
        }else {
            originator.setOriginatorId(originatorId);
            originatorMapper.updateByPrimaryKeySelective(originator);
            return originatorMapper.selectByPrimaryKey(originatorId);
        }
    }

    /**根据组织者ID获取组织者信息
     * @param originatorId
     * @return
     */
    @RequestMapping(value = "{originatorId}",method = RequestMethod.GET)
    public Originator get(@PathVariable int originatorId){
        Originator originator=originatorMapper.selectByPrimaryKey(originatorId);
        if (originator==null){
            throw new InvalidArgumentException("该组织者不存在，请核对originatorId是否正确");
        }else {
            return originator;
        }
    }


    /**获取组织者列表(可根据组织者状态)
     * @param originatorState
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public OriginatorPage query (@RequestParam(required = false)  Integer originatorState,@RequestParam(required = false) Integer pageNum){
        OriginatorExample example=new OriginatorExample();
        OriginatorExample.Criteria criteria=example.createCriteria();
        if (originatorState!=null){
            criteria.andOriginatorStateEqualTo(originatorState);
        }
        if (pageNum!=null){
            PageHelper.startPage(pageNum,15);
        }
        List<Originator> originatorList=originatorMapper.selectByExample(example);
        return originatorService.setOriginatorPage(originatorList);
    }

}
